package tech.blueglacier.configuration;

import org.apache.commons.collections4.map.CaseInsensitiveMap;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.XMLConfiguration;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Map;

public class AppConfig {

    private volatile static AppConfig objectInstance = null;

    private final Configuration appConfig;
    private CaseInsensitiveMap<String, String> charsetMap = null;

    private AppConfig() {
        try {
            this.appConfig = new XMLConfiguration(this.getClass().getClassLoader().getResource("conf/emailParserConfig.xml"));
        } catch (ConfigurationException e) {
            throw new RuntimeException(e);
        }
    }

    public static AppConfig getInstance() {
        if (objectInstance == null) {
            synchronized (AppConfig.class) {
                if (objectInstance == null) {
                    objectInstance = new AppConfig();
                }
            }
        }

        return objectInstance;
    }

    public Map<String, String> getCharsetMap() {
        if (charsetMap == null) {
            synchronized (AppConfig.class) {
                if (charsetMap == null) {
                    charsetMap = new CaseInsensitiveMap<>(10240);
                    String[] arrStr = appConfig.getStringArray("appSettings.charsetFallback.charset");
                    for (String arr : arrStr) {
                        int anchor = arr.lastIndexOf(':');
                        if (-1 == anchor) {
                            continue;
                        }

                        String parentCharSet = arr.substring(anchor + 1);
                        charsetMap.put(parentCharSet, parentCharSet);
                        for (String value : arr.substring(0, anchor).split(",", -1)) {
                            charsetMap.put(value, parentCharSet);
                        }
                    }
                }
            }
        }

        return charsetMap;
    }

    public boolean isImageFormat(String fileName) {
        if (!StringUtils.isEmpty(fileName)) {
            String[] arrImageFormat = appConfig.getStringArray("appSettings.imageFileFormats");
            return FilenameUtils.isExtension(fileName.toLowerCase(), arrImageFormat);
        }

        return false;
    }

    public static String getFallbackCharset(String charset) {
        return AppConfig.getInstance().getCharsetMap().getOrDefault(charset, getCompatibleCharset(charset));
    }

    private static String getCompatibleCharset(String charset) {
        for (String compatibleCharset : AppConfig.getInstance().getCharsetMap().keySet()) {
            if (charset.toLowerCase().contains(compatibleCharset.toLowerCase())) {
                String parentCharset = AppConfig.getInstance().getCharsetMap().get(compatibleCharset);
                AppConfig.getInstance().getCharsetMap().put(charset, parentCharset);
                return parentCharset;
            }
        }

        return charset;
    }
}
